define(function (require, exports) {
    var base = require("customize/ui/base");

    exports.init = function () {
//        $('.errorMsg').prev().bind('mouseover',function (e) {
//            var errorMsg = $(this).next();
//            if (errorMsg.html().length > 0 && errorMsg.find("label").html().length > 0) {
//                errorMsg.css('display', 'inline').css({'top': e.pageY + 10, 'left': e.pageX + 5});
//                if (errorMsg.find("label").css('display') == 'none') {
//                    errorMsg.hide();
//                }
//            }
//        }).bind('mouseout', function () {
//                $(this).next().css('display', 'none');
//            });

    };

    exports.errorPlacement = function (target, error, element) {
        if (error.html()) {
            $(element).addClass("x-form-invalid").addClass("error-border");
//            var errorMsg = $(element).addClass("error-border").closest("td").find(".errorMsg").hide().html(error);
//            if (!errorMsg.prev().is("span")) {
//                errorMsg.prev().addClass("x-form-invalid");
//            } else {
//                errorMsg.prev().children().addClass("x-form-invalid");
//            }
        }
    };

    exports.showErrors = function (target, errorMap, errorList) {
        if (errorList && errorList.length > 0) {
            $.each(errorList, function (index, obj) {
                $(obj.element).omTooltip({
                    region: 'right',
                    html: '<span style="color:red;">' + obj.message + '</span>',
                    anchor: true,
                    offset: [-5, 5]
                });

//                $(obj.element).closest("td").find(".errorMsg").html(obj.message).show();
            });
        } else {
            $(target.currentElements).omTooltip("destroy");
            $(target.currentElements).removeClass("error-border").removeClass("x-form-invalid");
//            $(target.currentElements).closest("td").map(function () {
//                var first = $(this).children().eq(0);
//                if (!first.is("span")) {
//                    first.removeClass("error-border").removeClass("x-form-invalid");
//                } else {
//                    first.children().removeClass("error-border").removeClass("x-form-invalid");
//                }
//                $(this).find(".errorMsg").hide();
//            });
        }
        target.defaultShowErrors();
    };

    // TODO 支持class配置验证
    var parseValidateRules = function () {
        //validate[required,custom[email]]
        $("*[class*='validate']").each(function () {
            var clazz = $(this).attr("class");

        });

        return {};
    };

    exports.validate = function (target, validateOptions) {
        target = base.jq(target);

        validateOptions = validateOptions || {};
        validateOptions.rules = validateOptions.rules || {};
        validateOptions.messages = validateOptions.messages || {};

        var rules = parseValidateRules();

        for (var key in rules) {
            validateOptions.rules[key] = rules[key];
        }

        target.removeData("validator");

        return target.validate({
            rules: validateOptions.rules,
            messages: validateOptions.messages,
            errorPlacement: function (error, element) {
                exports.errorPlacement(this, error, element);
            },
            showErrors: function (errorMap, errorList) {
                exports.showErrors(this, errorMap, errorList);
            }
        }).form();
    };
});